# -*- coding:utf-8 -*-
# @Author:🎈RedBalloon
# @Time:2022/9/29-21:27
# @File:07-ajax的请求.py
from urllib.request import urlopen, Request
from urllib.error import HTTPError
from fake_useragent import UserAgent
import time

url = "https://movie.douban.com/j/chart/top_list?type=13&interval_id=100%3A90&action=&start={}&limit=20"
# 一个计数器start
start = 0
while True:
    headers = {
        "Cookie": 'bid=OauyVuMQGXg; ll="118286"; __utmc=30149280; __utmz=30149280.1664457427.2.2.utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utmc=223695111; __utmz=223695111.1664457427.2.2.utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/; ap_v=0,6.0; _vwo_uuid_v2=D8C70EE784694B90F556968A8A876F6D4|bb49229f91aceb65fb0f35a0ab5598d2; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1664462260%2C%22https%3A%2F%2Fcn.bing.com%2F%22%5D; _pk_ses.100001.4cf6=*; __utma=30149280.2105355523.1653317622.1664457427.1664462260.3; __utmb=30149280.0.10.1664462260; __utma=223695111.1116633110.1653317622.1664457427.1664462260.3; __utmb=223695111.0.10.1664462260; _pk_id.100001.4cf6=d2f004556a4d370d.1653317621.3.1664462265.1664457942.',
        "Host": "movie.douban.com",
        "Referer": "https://movie.douban.com/typerank?type_name=%E7%88%B1%E6%83%85&type=13&interval_id=100:90&action=",
        "User-Agent": UserAgent().random,
    }
    base_url = url.format(start * 20)
    print(base_url)
    # Request() 构造请求头 传入请求头携带的参数
    request = Request(base_url, headers=headers)
    # 防止对方服务器压力过大,每隔3秒请求一次,(你好我好大家好)
    time.sleep(3)
    # urlopen() 请求网址(打开网址) 传入构造好的请求request
    response = urlopen(request)
    # 读取获取下来的数据(byte类型) 需要decode()下
    response_info = response.read()
    print(response_info.decode())
    # 判断响应是否为空或为None
    # 这样可以在获取完全部数据后不继续执行
    if response == "" or response is None:
        break
    start += 1
